// 要将loading注册为全局组件
import Vue from 'vue'
// 引入组件
import Loading from '@/components/Loading.vue'

const Createloading = Vue.extend(Loading)
// 实例化组件 将该组件的挂载点设置为一个自己创建的div
const instence = new Createloading({
  el: document.createElement('div')
})
// // instence 是一个实例化对象 给当前实例添加一个自定义属性
instence.show = false

let time = null

const loading = {
  show(tips) {
    instence.show = true
    document.body.appendChild(instence.$el)// $el 其实就是挂载点的dom
    time = new Date()
  },
  hide() {
    const current = new Date()
    if (current - time < 1200) {
      setTimeout(() => {
        instence.show = false
      }, 1200 - (current - time))
    } else {
      instence.show = false
    }
  }
}

export default loading

